﻿using System.Collections.Generic;
using System.Linq;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.BusinessObjects;
using com.eforceglobal.crux.BusinessObjects.DatabaseQueries;
using com.eforceglobal.crux.exceptions;

namespace com.eforceglobal.crux.BusinessFacades
{
    public class LocationFacade
    {
        public List<LocationInformation> GetLocations(int CompanyID)
        {
            Queries locationsQuery = new Queries();
            IEnumerable<LocationInformation> locations = 
                from locationsList in locationsQuery.GetLocations(CompanyID)
                select ClassConverter.CopyLocationToLocationInfomration(locationsList);
            return locations.ToList();
        }

        public List<LocationInformation> GetAllLocations()
        {
            Queries locationsQuery = new Queries();
            IEnumerable<LocationInformation> locations =
                from locationsList in locationsQuery.GetAllLocations()
                select ClassConverter.CopyLocationToLocationInfomration(locationsList);
            return locations.ToList();
        }


        public LocationInformation GetLocation(int LocationID)
        {
            if (LocationID == 0) return new LocationInformation();
            Queries locationQuery = new Queries();
            Location location = locationQuery.GetLocation(LocationID);
            return ClassConverter.CopyLocationToLocationInfomration(location);
        }

        public void SaveLocation(LocationInformation location)
        {
            CheckValidity(location);
            Queries locationQuery = new Queries();
            Location dbLocation = new Location();
            if(location.LocationID!=0)
                dbLocation = locationQuery.GetLocation(location.LocationID);
            dbLocation = ClassConverter.CopyLocationInformationToLocation(location, dbLocation);
            if (location.LocationID != 0)
                locationQuery.SubmitChanges();
            else
                locationQuery.InsertLocation(dbLocation);

        }

        private void CheckValidity(LocationInformation location)
        {
            if (DoesLocationExist(location, location.CompanyName))
                throw CruxExceptionManager.CreateNewDatabaseException("903");
        }

        public bool DoesLocationExist(LocationInformation location, string CompanyName)
        {
            Queries query = new Queries();
            Location dbLocation = query.GetLocation(location.LocationName, CompanyName);
            if (dbLocation != null && dbLocation.LocationID != location.LocationID)
                return true;
            return false;

        }

        public void DeleteLocation(int LocationID)
        {
            Queries locationQuery = new Queries();
            Location location = locationQuery.GetLocation(LocationID);
            locationQuery.DeleteLocation(location);
        }

        public List<LocationInformation> GetAllInternalLocations()
        {
            Queries locationQuery = new Queries();
            IEnumerable<LocationInformation> locations = from alllocations in locationQuery.GetLocationsByCompanyType("InternalCompany")
                                                         select ClassConverter.CopyLocationToLocationInfomration(alllocations);
            return locations.ToList();
        }

       

        public List<LocationInformation> GetLocationsRepresentedBy(int RepresentativeID)
        {
            Queries query = new Queries();
            IEnumerable<LocationInformation> locations = from alllocations in query.GetLocationsRepresentedBy(RepresentativeID)
                                                         select ClassConverter.CopyLocationToLocationInfomration(alllocations);
            return locations.ToList();

        }
    }
}
